﻿using System;
using System.Windows.Forms;

using System.Linq;
using System.Reflection;
using System.IO;
using Conversion;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Button b = new Button();
            typeof(BitConverter).GetRuntimeField("IsLittleEndian").SetValue(null, false);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //double d = 0d;
            //var t1 = System.DateTime.Now.Ticks;
            //for (int i = 0; i < 1000000; i++)
            //{
            //    d = 3.0 / 1 * 100.1;
            //}
            //var t2 = System.DateTime.Now.Ticks;
            //TimeSpan elapsedSpan = new TimeSpan(t2 - t1);
            //this.textBox1.Text = d.ToString() + "|" + elapsedSpan.TotalMilliseconds.ToString();
            //BinaryReader br = new BinaryReader("");
            
            var dou = Convert.ToDouble(textBox1.Text);
            long m = BitConverter.DoubleToInt64Bits(dou); // 将 double 转成 Int64
            //var bytes = BitConverter.GetBytes(m);
            var bigConverter = EndianBitConverter.Big;
            var bytes =   bigConverter.GetBytes(dou);
            //string str = Convert.ToString(m, 10); // 将 Int64 转成二进制字符串
            appendResult("double to byte: " + string.Join(",", bytes));

            appendResult(BitConverter.IsLittleEndian.ToString());

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var txt = textBox1.Text;
            var bs = txt.Split(',').Select(t => Convert.ToByte(t)).ToArray();
            var dou = BitConverter.ToDouble(bs, 0);
            appendResult("bytes to double: " + dou.ToString());
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void appendResult(string txt)
        {
            txtResult.AppendText(txt);
            txtResult.AppendText(System.Environment.NewLine);
        }
    }
}
